import requests
from bs4 import BeautifulSoup
# 进行请求
response = requests.get("https://www.hbjcxy.com/jyyw/")
# 设置编码方式
response.encoding = "utf-8"
# 解析网页
soup = BeautifulSoup(response.text,features="html.parser")
# 取得所有的item
items = soup.select(".list-item")
# print(items)
data = []
for item in items:
    title = item.select(".list-title")[0].text.strip()
    date = item.select(".list-meta")[0].text.strip()
    print(title)
    print(date)
    d = {"标题":title,"时间":date}
    data.append(d)
print(data)
import pymysql
con = pymysql.connect(host="localhost",user="root",password="123456",database="spider")
cursor = con.cursor()
sql = "insert into news values(NULL,%s,%s)"
for d in data:
    cursor.execute(sql,list(d.values()))
con.commit()
cursor.close()
con.close()
